<?php
namespace app\sisome\controller;
use think\Controller;
use think\Db;
// use think\Request;
class Common extends Controller{
    public function _initialize(){
    	if(request()->isPost() && !request()->isAjax()){
    		if (!isset($_POST['__token__'])) {
    			$this->error('非法请求！');
    		}else{
				 if(session('__token__')<>$_POST['__token__']){
				 	// $this->error('请勿重复提交！');
				 }
    		}
		}
        //限制登录ip
        $ip = $this->ip();
        //获取设置的ip地址
        $allow_ip = setting('allow_ip');
        if(!in_array($ip,explode(',',$allow_ip)) && $ip!='127.0.0.1'){
            echo "地址未授权";
            exit;
        }
   		if(!islogin_admin()){
   			if(request()->module().request()->controller().request()->action()=='sisomeIndexindex'){
   				$this->redirect('publics/login');
   			}else{
   				$this->error('请先登陆','publics/login');
   			}
		}else{
			$this->checkSession();

   			$setting=setting();
   			$aduser=db('aduser')->where(array('id'=>session('aduid')))->find();
   			$this->setting=$setting;		$this->assign('setting',$setting);
			$this->aduser=$aduser;			$this->assign('aduser',$aduser);
		}
    }

    /**
	 *
	 * 功能：10分钟不操作退出登录
	 */
	Public function checkSession(){
		// dump(session(''));	//打印所有session
		$dietime=session('adsession_id_dietime');	//设置了记住密码 失效时间到后登出
		if(!$dietime){
			$dietime=session('adlasttime')+60*30;	//否则 30分钟无操作登出
		}
		if($dietime>time()){
			session('adlasttime', time());
		}else{
			echo "<script> localStorage.clear(); </script>";        //清除本地账号密码存储
	        session(null);
	        cookie('PHPSESSID',null);
	        $this->error('登陆超时，请重新登陆！','publics/login');
		}
	}

    public function ip() {
        //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else if (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
        //dump(phpinfo());//所有PHP配置信息
    }

    /**
     * @name 交易取消
     * @param $order_id
     * @throws \Exception
     * @throws \think\Exception
     */
    public function trade_cancel($order_id){
        $oob = Db('ppdd_jiaoyi')->where(['id' => $order_id])->whereIn('zt',[1,5])->find();
        if (empty($oob)) {
            throw new \Exception('交易不存在或不能取消');
        }
        $res = Db('ppdd_jiaoyi')->where(['id' => $oob['id']])->update(['zt' => 3, 'update_time' => time()]);
        if (!$res) {
            throw new \Exception('状态变更失败');
        }
        //取消求购单表
        $res = Db('ppdd')->where(['id' => $oob['p_order_id'], 'zt' => 1])->update(['zt' => 3, 'update_time' => time(), 'cancel_time' => time()]);
        if (!$res) {
            throw new \Exception('求购单状态修改失败');
        }
        //卖家冻结idc解冻
        $res = DB('user')->where(['id' => $oob['sell_id']])->setDec('idc_freeze', ($oob['num'] + $oob['fee']));
        if (!$res) {
            throw new \Exception('卖家解冻币失败');
        }
    }
}